package sctu.edu.java.year2016.team01.OurTeamProject.subroutine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zero on 2016/6/26.
 */
public class Read {

    String path = "src\\main\\java\\sctu\\edu\\java\\year2016\\team01\\OurTeamProject";

    public List<String> read() {



        BufferedReader reader = null;
        String tempString = null;
        List<String> list = new ArrayList<>();

        try {
            reader = new BufferedReader(new FileReader("D:/user.txt"));
            while ((tempString = reader.readLine()) != null) {

                int i = 0;
                String[] read = tempString.split("\\*");
                list.add(read[i++]);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }


    public List<String> txtName(String string){


        BufferedReader reader = null;
        String tempString = null;
        List<String> list = new ArrayList<>();

        try {
            reader = new BufferedReader(new FileReader(string));
            while ((tempString = reader.readLine()) != null) {

                int i = 0;
                String[] read = tempString.split("\\s+");
                list.add(read[i++]);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (Exception e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }

    public void solution(String txtName) {




        File file = new File("D:/" + txtName);
        JFrame f = new JFrame();
        f.setLayout(null);
        f.setTitle("解决方案");
        f.setBounds(800, 300, 800, 600);
        Container con = f.getContentPane();
        JPanel pan = new JPanel();

        ImageIcon background = new ImageIcon(path + "\\背景.png");
        JLabel label = new JLabel(background);
        label.setBounds(0, 0, 800, 600);
        JPanel imagePanel = (JPanel) f.getContentPane();
        imagePanel.setOpaque(false);
        f.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

        JLabel title = new JLabel("请输入你解决的方案:");
        title.setFont(new Font("宋体", Font.BOLD, 21));
        title.setForeground(Color.white);
        f.getContentPane().add(title);
        title.setBounds(100, 50, 300, 40);

        JTextArea solutionContent=new JTextArea();
        solutionContent.setBounds(100, 100, 400, 300);
        solutionContent.setFont(new Font("宋体", Font.BOLD, 21));
        f.getContentPane().add(solutionContent);

        JButton submit=new JButton("提交");
        submit.setFont(new Font("宋体", Font.BOLD, 21));
        submit.setContentAreaFilled(false);
        submit.setForeground(Color.white);
        submit.setBounds(250, 410, 100, 30);
        f.getContentPane().add(submit);


        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                RegisterSuccess a=new RegisterSuccess();
                BufferedWriter writer = null;


                try {



                    FileWriter save = new FileWriter(file, true);
                    writer = new BufferedWriter(save);
                    writer.write(solutionContent.getText() + "\r\n\r\n");
                    a.registerSuccess("提交成功！");
                    f.dispose();



                } catch (Exception e1) {
                    e1.printStackTrace();
                } finally {
                    try {
                        writer.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }


            }
        });





        con.add(pan);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    }
}
